#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "device.h"
#include <time.h>

extern DeviceManager *head;
extern pthread_mutex_t log_mutex;
extern volatile sig_atomic_t keep_running;

void signal_handler(int sig) {
    keep_running = 0;  // 通知线程池停止
}

int main() {
    // 清空 device.log 文件
    FILE *log_file = fopen("device.log", "w");
    if (!log_file) {
        perror("无法清空 device.log");
        return 1;
    }
    fclose(log_file);

    // 设置 SIGINT 信号处理
    signal(SIGINT, signal_handler);

    srand(time(NULL));
    add_device(SENSOR_TEMP, "温度传感器");
    add_device(SENSOR_HUMIDITY, "湿度传感器");
    add_device(SENSOR_LIGHT, "光照传感器");
    add_device(ACTUATOR_AC, "空调");
    add_device(ACTUATOR_HUMIDIFI, "加湿器");
    add_device(ACTUATOR_LIGHT, "灯光");

    start_thread_pool();

    free_all_devices();
    pthread_mutex_destroy(&log_mutex);
    return 0;
}